import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
    state: () => ({
        token: sessionStorage.getItem('token') || '',
        userInfo: JSON.parse(sessionStorage.getItem('userInfo') || null)
    }),
    actions: {
        setToken(token) {
            this.token = token
            sessionStorage.setItem('token', token)
        },
        setUserInfo(userInfo) {
            this.userInfo = userInfo
            sessionStorage.setItem('userInfo', JSON.stringify(userInfo))
        },
        logout() {
            this.token = ''
            this.userInfo = null
            sessionStorage.removeItem('token')
            sessionStorage.removeItem('userInfo')
        }
    }
})